$(function(){
	pgjump(15,1,'indicator_id desc');
});
//上一页、下一页，点击数字页
function paging(pageNo){
	var pageSize=15;
	if($("#pageSize").val()!=null){
		pageSize = $("#pageSize").val();
	}
	pgjump(pageSize,pageNo,'');
}
//查询
function searchPaging(){
	var pageSize=$("#pageSize").val();
	pgjump(pageSize,1,"indicator_id desc");
}
//分页查询
function pgjump(pageSize,pageNo,orderField){
	$.ajax({
		url: "index_paging.do",
		type:"post",  
        dataType: 'json',
		data:{
			"iPageSize":pageSize,
			"iPageNo":pageNo,
			"orderField":orderField,
			"name":$("#partName").val(),
			"bcode":$("#bcode").val(),
			"partsPO.partsId":$("#partsId").val(),
			"lookupCdePO.lookupCode":$("#type").val()
			},
		error:function(data){
			layer.msg('出错了！！',{time:2000});
	    },
		success: function(jsons) {
			if(jsons!=null){
				$("#partsForm").html("");
				$("#partsPageList").html("");
				var pageList=jsons.pageData.pageList;
				for(var i=0;i<pageList.length;i++ ){
					var partsForm=
					'<tr>'+
				        '<td><input type="checkbox" onclick="manucheck(this);" value="'+pageList[i].indicatorId+'" '+(pageList[i].exisUsing?"disabled":"name='pageItem'")+' /></td>'+
				        '<td title="'+pageList[i].name+'">'+pageList[i].name+'</td>'+
				        //name删除时获取bcode
				        '<td name="bcode" title="'+pageList[i].bcode+'">'+pageList[i].bcode+'</td>'+
				        '<td title="'+pageList[i].lookupCdePO.cdeName+'">'+pageList[i].lookupCdePO.cdeName+'</td>'+
				        '<td title="'+pageList[i].partsPO.name+'">'+pageList[i].partsPO.name+'</td>'+
				        '<td>'+(pageList[i].exisUsing?'用户已订制，不可删除!<img src="../img/Flexigrid/eye.png" border="0" style="cursor:pointer" onclick="detailUseUser('+pageList[i].userIdStr+')"/>':"")+'</td>'+
				        '<td><i class="Edit content_current" onclick="editIndicators('+JSON.stringify(pageList[i]).replace(/\"/g,"'")+')" title="编辑"></i></td>'+//<i class="See content_current" onclick="detailIndicators('+pageList[i].id+')" title="查看"></i>
			    	'</tr>';
			    	$(partsForm).appendTo("#partsForm");
				}
			}
			dealFooterPage(jsons.pageData.totalCount,jsons.pageData.pageSize,jsons.pageData.totalPage,jsons.pageData.pageNo);
		}
	});
}
//全选框选择
$(".checkAll").change(function (){
	$(":checkbox[name='pageItem']").attr("checked",this.checked);
})
//同步全选框
function manucheck(field){
	if(field.checked==false){
		$(".checkAll").removeAttr("checked");
	}else{
		var flagCheckAll = true;
		$("input[name='pageItem']").each(function(){
			if($(this).attr("checked")!='checked'){
				flagCheckAll=false;
			} 
		});
		if(flagCheckAll){
			$(".checkAll").attr("checked","checked");
		}
	}
}
//添加
function addIndicators(){
    layer.open({
    	extend:['skin/layer_iframe.css'],
        skin:'layer-ext-skin-layer_iframe.css',
        type:2,
        title:'添加指标项',
        area:['1000px','500px'],

        content:'initAddIndicator.do',
        maxmin: true,
        shadeClose: true

    });
}
//添加删除指标后刷新部件
function flashParts(){
	$.ajax({
		url: "flashParts.do",
		type:"post",  
        dataType:'json',
        async:false,
		error:function(data){  
			layer.msg('出错了！！',{time:2000});
	    },
		success: function(result) {
			var jsonParts = eval('('+result.strParts+')');
			var partsHtml = "<option value=''>请选择...</option>";
			for (var i = 0; i < jsonParts.length; i++) {
				partsHtml+="<option value='"+jsonParts[i].partsId+"'>"+jsonParts[i].name+"</option>";
			}
			$("#partsId").html(partsHtml);
		}
	});
}
//删除
function deleteIndicators(){
	var IndicatorIds=$("input:checkbox[name='pageItem']:checked");
	if(IndicatorIds.length==0){
		layer.msg('请选择要删除的指标', {time:2000});
	}else{
		//询问框
		layer.confirm('确认删除吗？', {
		    btn: ['确定','取消'] //按钮
		}, function(){
			var bcode = [];
			var ids = [];
			$.each(IndicatorIds, function() {
				ids.push(this.value);
				bcode.push($(this).parent().parent().find("td[name='bcode']").eq(0).text());
			});
			var idString =ids.join(",");
			var bcodeStr = bcode.join(",");
			$.ajax({
				url: "deleteIndicators.do",
				type:"post",  
		        dataType: 'json',
				data:{
					"idStr":idString,
					"bcodeStr" :bcodeStr
				},
				error:function(data){
					layer.msg('出错了！！',{time:2000});
				},
				success:function(result){
					$("#checkAll").attr("checked",false);
					if(result.flag){
						layer.msg('删除成功', {icon: 1});
						flashParts();
						pgjump($("#pageSize").val(),1,"indicator_id desc");
					}else{
						layer.msg('删除失败');
					}
				}
			});
		}, function(){
		    //layer.msg('奇葩么么哒', {shift: 6});
		});
	}
}
//修改
function editIndicators(indVOStr){
	layer.open({
    	extend:['skin/layer_iframe.css'],
        skin:'layer-ext-skin-layer_iframe.css',
        type:2,
        title:'修改指标项',
        area:['1000px','500px'],

        content:"initEditIndicator.do?indVOStr="+encodeURI(encodeURI(JSON.stringify(indVOStr))),
        maxmin: true,
        shadeClose: true

    });
}
function detailUseUser(userIds){
	$.dialog.open("/userBingIndicators.mo?userIds="+userIds,{
		title:"查看订制用户",
		width:350,
		height:400,
		lock:true
	});	
}
function detailIndicators(indicatorsId){
	$.dialog.open("/indicatorsMgrDetail.mo?indicatorsId="+indicatorsId,{
		title:"查看指标项",
		width:450,
		height:400,
		lock:true
	});	
}
